package com.autumn.zero.workflow.entitys;

import javax.persistence.Column;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.apache.ibatis.type.JdbcType;
import org.hibernate.validator.constraints.Length;

import com.autumn.common.api.constants.SimpleDataTypeConstant;
import com.autumn.constants.SettingConstants;
import com.autumn.domain.entities.AbstractDefaultEntity;
import com.autumn.mybatis.mapper.annotation.ColumnOrder;
import com.autumn.mybatis.mapper.annotation.ColumnType;
import com.autumn.mybatis.mapper.annotation.Index;
import com.autumn.util.ExceptionUtils;
import com.autumn.validation.annotation.NotNullOrBlank;

/**
 * 工作流表单属性
 * 
 * @author 老码农 2018-12-19 22:48:38
 */
@Table(name = SettingConstants.SYS_TABLE_PREFIX + "_workflow_form_property")
public class WorkflowFormProperty extends AbstractDefaultEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4692751196562153568L;

	/**
	 * 最大属性长度
	 */
	public static final int MAX_PROPERTY_NAME_LENGTH = 50;

	/**
	 * 最大友好名称长度
	 */
	public static final int MAX_FRIENDLY_NAME_LENGTH = 50;
	
	/**
	 * 字段 formId
	 */
	public static final String FILED_FORM_ID = "formId";
	
	/**
	 * 字段 propertyName
	 */
	public static final String FILED_PROPERTY_NAME = "propertyName";
	
	/**
	 * 字段 friendlyName
	 */
	public static final String FILED_FRIENDLY_NAME = "friendlyName";
	
	/**
	 * 字段 propertyType
	 */
	public static final String FILED_PROPERTY_TYPE = "propertyType";

	/**
	 * 表单类型Id
	 */
	@Index
	@Column(name = "form_Id", nullable = false)
	private Long formId;

	/**
	 * 属性名称
	 */
	@NotNullOrBlank(message = "属性不能为空")
	@Length(max = MAX_PROPERTY_NAME_LENGTH, message = "属性不能为空长度不能超过" + MAX_PROPERTY_NAME_LENGTH + "个字。")
	@Column(name = "property_name", nullable = false, length = MAX_PROPERTY_NAME_LENGTH)
	@ColumnType(jdbcType = JdbcType.VARCHAR)
	@Index
	@ColumnOrder(1)
	private String propertyName;

	/**
	 * 友好名称
	 */
	@NotNullOrBlank(message = "属性友好不能为空")
	@Length(max = MAX_FRIENDLY_NAME_LENGTH, message = "属性友好名称不能为空长度不能超过" + MAX_FRIENDLY_NAME_LENGTH + "个字。")
	@Column(name = "friendly_name", nullable = false, length = MAX_FRIENDLY_NAME_LENGTH)
	@ColumnType(jdbcType = JdbcType.VARCHAR)
	@ColumnOrder(2)
	private String friendlyName;

	/**
	 * 属性类型 {@link com.autumn.common.api.constants.SimpleDataTypeConstant}
	 */
	@NotNull(message = "属性类型不能为空")
	@Column(name = "property_type", nullable = false)
	@ColumnOrder(3)
	private Integer propertyType;

	/**
	 * 获取表单id
	 * 
	 * @return
	 */
	public Long getFormId() {
		return formId;
	}

	/**
	 * 设置表单Id
	 * 
	 * @param formId
	 *            类型Id
	 */
	public void setFormId(Long formId) {
		this.formId = formId;
	}

	/**
	 * 获取属性名称
	 * 
	 * @return
	 */
	public String getPropertyName() {
		return propertyName;
	}

	/**
	 * 设置属性名称
	 * 
	 * @param propertyName
	 *            属性名称
	 */
	public void setPropertyName(String propertyName) {
		this.propertyName = propertyName;
	}

	/**
	 * 获取友好名称
	 * 
	 * @return
	 */
	public String getFriendlyName() {
		return friendlyName;
	}

	/**
	 * 设置友好名称
	 * 
	 * @param friendlyName
	 *            友好名称
	 */
	public void setFriendlyName(String friendlyName) {
		this.friendlyName = friendlyName;
	}

	/**
	 * 获取属性类型
	 * 
	 * @return {@link com.autumn.common.api.constants.SimpleDataTypeConstant}
	 */
	public Integer getPropertyType() {
		return propertyType;
	}

	/**
	 * 设置属性类型
	 * 
	 * @param propertyType
	 *            属性类型
	 *            {@link com.autumn.common.api.constants.SimpleDataTypeConstant}
	 */
	public void setPropertyType(Integer propertyType) {
		this.propertyType = propertyType;
	}

	@Override
	public void valid() {
		super.valid();
		if (!SimpleDataTypeConstant.exist(this.getPropertyType())) {
			ExceptionUtils.throwValidationException("指定的属性数据类型无效。");
		}
	}

}
